/**
 * Title: ConfigUtil.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-19 下午5:48:23
 * Version 1.0
 */
package com.luoxudong.phonenumber.logic.utils;

import java.io.File;

import com.google.gson.Gson;
import com.luoxudong.phonenumber.logic.common.BaseConfig;
import com.luoxudong.phonenumber.service.utils.FileUtil;

/** 
 * ClassName: ConfigUtil
 * Description:配置信息操作工具类
 * Create by 罗旭东
 * Date 2013-8-19 下午5:48:23
 */
public class ConfigUtil {
	/**
	 * 系统基础配置文件
	 */
	private static final String SYS_CONFIG_FILE_NAME = "sys_config";
	
	/**
	 * 用户基本配置信息
	 */
	private static final String USER_CONFIG_FILE_NAME = "user_config";
	
	private static byte[] synByte = new byte[1];
	
	public static SystemConfigInfo systemConfigInfo = null;
	
	public static UserConfigInfo userConfigInfo = null;
	
	private static void checkSystemConfig()
	{
		if (systemConfigInfo == null)
		{
			readSystemConfig();
			if (systemConfigInfo == null)
			{
				systemConfigInfo = new ConfigUtil().new SystemConfigInfo();
				systemConfigInfo.setLoginInfo(new ConfigUtil().new LoginInfo());
				systemConfigInfo.setSystemInfo(new ConfigUtil().new SystemInfo());
			}
			
		}
	}
	
	private static void checkUserConfig()
	{
		if (userConfigInfo == null)
		{
			readUserConfig();
			
			if (userConfigInfo == null)
			{
				userConfigInfo = new ConfigUtil().new UserConfigInfo();
				userConfigInfo.setUserSetInfo(new ConfigUtil().new UserSetInfo());
			}
			
		}
	}
	
	private static void readSystemConfig()
	{
		File file = new File(BaseConfig.APP_ROOT_DIR, SYS_CONFIG_FILE_NAME);
		if (!file.exists())
		{
			systemConfigInfo = null;
			return;
		}
		String systemConfigJson = FileUtil.readFileFromSDCard(BaseConfig.APP_ROOT_DIR, SYS_CONFIG_FILE_NAME);
		
		Gson gson = new Gson();
		systemConfigInfo = gson.fromJson(systemConfigJson, SystemConfigInfo.class);
	}
	
	private static void readUserConfig()
	{
		File file = new File(BaseConfig.USER_CONFIG_DIR, USER_CONFIG_FILE_NAME);
		if (!file.exists())
		{
			userConfigInfo = null;
			return;
		}
		
		String userConfigJson = FileUtil.readFileFromSDCard(BaseConfig.USER_CONFIG_DIR, USER_CONFIG_FILE_NAME);
		
		Gson gson = new Gson();
		userConfigInfo = gson.fromJson(userConfigJson, UserConfigInfo.class);
	}
	
	private static void writeSystemConfig()
	{
		File file = new File(BaseConfig.APP_ROOT_DIR);
		
		if (!file.exists())
		{
			file.mkdirs();
		}
		
		checkSystemConfig();
		
		Gson gson = new Gson();
		String systemConfigJson = gson.toJson(systemConfigInfo);
		synchronized (synByte) 
		{
			try {
				FileUtil.saveFileToSDCard(BaseConfig.APP_ROOT_DIR, SYS_CONFIG_FILE_NAME, systemConfigJson);
			} catch (Exception e) {
			}
		}
		
	}
	
	private static void writeUserConfig()
	{
		File file = new File(BaseConfig.USER_CONFIG_DIR);
		
		if (!file.exists())
		{
			file.mkdirs();
		}
		
		checkUserConfig();
		
		Gson gson = new Gson();
		String userConfigJson = gson.toJson(userConfigInfo);
		synchronized(synByte)
		{
			try {
				FileUtil.saveFileToSDCard(BaseConfig.USER_CONFIG_DIR, USER_CONFIG_FILE_NAME, userConfigJson);
			} catch (Exception e) {
			}
		}
	}
	
	public static UserConfigInfo getUserConfigInfo()
	{
		checkUserConfig();
		return userConfigInfo;
	}
	
	/**
	 * 获取登陆名
	 * @return
	 */
	public static String getUserName()
	{
		checkSystemConfig();
		return systemConfigInfo.getLoginInfo().getUsername();
	}
	
	/**
	 * 设置登陆账号
	 * @param username
	 */
	public static void setUserName(String username)
	{
		checkSystemConfig();
		systemConfigInfo.getLoginInfo().setUsername(username);
		writeSystemConfig();
	}
	
	/**
	 * 获取密码
	 * @return
	 */
	public static String getPassword()
	{
		checkSystemConfig();
		return systemConfigInfo.getLoginInfo().getPassword();
	}
	
	/**
	 * 设置密码
	 * @param password
	 */
	public static void setPassword(String password)
	{
		checkSystemConfig();
		systemConfigInfo.getLoginInfo().setPassword(password);
		writeSystemConfig();
	}
	
	/**
	 * 获取上一个版本号
	 * @return
	 */
	public static int getLatestVersionNo()
	{
		checkUserConfig();
		return userConfigInfo.getLatestDbVersion();
	}
	
	/**
	 * 设置上一个版本号
	 * @param latestVersionNo
	 * @return
	 */
	public static void setLatestVersionNo(int latestVersionNo)
	{
		checkUserConfig();
		userConfigInfo.setLatestDbVersion(latestVersionNo);
		writeUserConfig();
	}
	
	/**
	 * 获取上次同步地区时间
	 * 
	 * @return
	 */
	public static String getLastAreaUpdateTime() {
		checkUserConfig();
		
		return userConfigInfo.getLastAreaUpdateTime();
	}

	/**
	 * 设置上一次同步地区时间
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void setLastAreaUpdateTime(String value) {
		
		checkUserConfig();
		
		userConfigInfo.setLastAreaUpdateTime(value);
		
		writeUserConfig();
	}

	
	
	public class SystemConfigInfo
	{
		private LoginInfo loginInfo = null;
		
		private SystemInfo systemInfo = null;

		public LoginInfo getLoginInfo() {
			return loginInfo;
		}

		public void setLoginInfo(LoginInfo loginInfo) {
			this.loginInfo = loginInfo;
		}

		public SystemInfo getSystemInfo() {
			return systemInfo;
		}

		public void setSystemInfo(SystemInfo systemInfo) {
			this.systemInfo = systemInfo;
		}
		
	}
	
	public class UserConfigInfo
	{
		/**
		 * 用户设置信息
		 */
		private UserSetInfo userSetInfo = null;

		/**
		 * 最新数据库版本号
		 */
		private int latestDbVersion = -1;
		
		/**
		 * 上次同步时间
		 */
		private String lastAreaUpdateTime = null;
		
		public UserSetInfo getUserSetInfo() {
			return userSetInfo;
		}

		public void setUserSetInfo(UserSetInfo userSetInfo) {
			this.userSetInfo = userSetInfo;
		}
		
		public int getLatestDbVersion() {
			return latestDbVersion;
		}

		public void setLatestDbVersion(int latestDbVersion) {
			this.latestDbVersion = latestDbVersion;
		}

		public String getLastAreaUpdateTime() {
			return lastAreaUpdateTime;
		}

		public void setLastAreaUpdateTime(String lastAreaUpdateTime) {
			this.lastAreaUpdateTime = lastAreaUpdateTime;
		}
	}
	
	public class LoginInfo
	{
		/**
		 * 登陆账号
		 */
		private String username = null;
		
		/**
		 * 登陆密码
		 */
		private String password = null;
		
		public String getUsername() {
			return username;
		}
		
		public void setUsername(String username) {
			this.username = username;
		}
		
		public String getPassword() {
			return password;
		}
		
		public void setPassword(String password) {
			this.password = password;
		}
	}
	
	public class SystemInfo
	{
		
	}
	
	public class UserSetInfo
	{
		
	}
}
